package com.smile.Transform;

import org.apache.flink.api.common.functions.FilterFunction;
import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;


public class FlinkTransForm {
    public static void main(String[] args) throws Exception {
        //配置环境
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setParallelism(1);
        //加载数据源
        DataStream<String> dataStream
                = env.readTextFile(
                        "E:\\flink\\HotItemsAnalysis\\src\\main\\resources\\a.txt");
        DataStreamSource<String> dataStream2
                = env.readTextFile(
                        "E:\\flink\\HotItemsAnalysis\\src\\main\\resources\\UserBehavior.csv");
        dataStream.print();
        //转换算子(map)
        DataStream<Integer> mapStream = dataStream.map(new MapFunction<String, Integer>() {
            public Integer map(String value) throws Exception {
                return value.length();
            }
        });
        mapStream.print();
        //flatmap
        DataStream<Object> flatMap = dataStream2.flatMap(new FlatMapFunction<String, Object>() {
            @Override
            public void flatMap(String value, Collector<Object> collector) throws Exception {
                String[] fields = value.split(",");
                for (String field : fields) {
                    collector.collect(field);
                }
            }
        });
        //filtermap
//        DataStream<Interger> filterStream = dataStream.filter(new FilterFunction<String>()
//        {
//            public boolean filter(String value) throws Exception {
//                return value == 1;
//            }
//        });


        env.execute();
    }

}
